﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using Tools;

namespace DemoWPF
{
	public class Model : Model<Item> { public Model() : base() { } }
	public class Model<T>
		where T : INotifyPropertyChanging
	{
		public ListCollectionView View { get; private set; }
		public ICommand AddNewCommand { get; private set; }
		public ICommand RemoveCommand { get; private set; }
		public ICommand UndoCommand { get; private set; }
		public ICommand RedoCommand { get; private set; }
		
		public Model()
		{
			var collection = new ObservableCollection<T>();
			var undoRedo = new UndoRedo();
			undoRedo.Subscribe<T>(collection);
			
			View = new ListCollectionView(collection);
			
			AddNewCommand = new RelayCommand(p => { View.AddNew(); View.CommitNew(); },
			                                 p => View.CanAddNew);
			
			RemoveCommand = new RelayCommand(p => View.Remove(View.CurrentItem),
			                                 p => View.CanRemove && View.CurrentItem != null);
			
			UndoCommand = new RelayCommand(p => undoRedo.Undo(),
			                               p => undoRedo.CanUndo);
			
			RedoCommand = new RelayCommand(p => undoRedo.Redo(),
			                               p => undoRedo.CanRedo);
		}
	}
}
